package com.mengxuegu.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.mengxuegu.springboot.entities.*;
import com.mengxuegu.springboot.mapper.BankBillMapper;
import com.mengxuegu.springboot.service.BankUserService;
import com.mengxuegu.springboot.service.ItemRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;


@RequestMapping("/bill")
@RestController
public class BankBillController {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    BankBillMapper bankBillMapper;
    @Autowired
    BankUserService bankUserService;
    @Autowired
    ItemRepository itemRepository;
    @PostMapping("/gettransBynum")
    public Object gettransBynum(@RequestBody TransBean bean){
        //账单查询
        logger.info("+++++"+bean.getNum());
        return bankBillMapper.gettransBynum(bean.getNum());
    }

    @PostMapping("/addBanktrans")
    public boolean addBanktrans(@RequestBody TransBean bean){
        //借款还款
        //1 TODO 根据卡号判断是否存在
        logger.info("+++++"+bean.getNum());
        CardBean cardBean=new CardBean();
        cardBean.setNum(Integer.parseInt(bean.getNum()));
        Object o=bankUserService.getBillNum(cardBean);
        logger.info("返回："+ JSON.toJSONString( o));
        if(null==o){
            logger.info("卡号不存在："+bean.getNum());
            return false;
        }
        //2 新增记录
        int i = bankBillMapper.addBanktrans(bean);
        if(i > 0){
            if(null!=bean.getQmoney()){
                //3 借款后 更新 借款后修改图（统计图）
                SpendBean spendBean  =new SpendBean();
                spendBean.setNum(bean.getNum());
                List<SpendBean> spendBeans= bankBillMapper.getSpend(spendBean);
                if(!spendBeans.isEmpty()){
                    SpendBean spendBean1 =  spendBeans.get(0);
                    BigDecimal xf= spendBean1.getXf();//原始值
                    spendBean1.setXf(xf.add(bean.getQmoney()) );//加上消费
                    bankBillMapper.updateSpendByNum(spendBean1);
                }
            }
            return true;
        }
        return false;
    }

    @PostMapping("/getSpend")
    public Object getSpend(@RequestBody SpendBean spendBean){
        //统计图
        return bankBillMapper.getSpend(spendBean);
    }

    @PostMapping("/getSpendAll")
    public Object getSpendAll(@RequestBody SpendBean spendBean){
        //统计图
        Result result = new Result();
//        return bankBillMapper.getSpendAll(spendBean);
//        Iterable<SpendBean> all = itemRepository.findAll((Pageable) bankBillMapper.getSpendAll(spendBean));
//        List<SpendBean> list = new ArrayList<>();
//        all.forEach(list::add);
//        for(SpendBean spend:all){
//            System.out.println("++++++"+spend);
//        }
        result.setCode("1");
        result.setMessage("success");
        return result;


    }

    @PostMapping("/updateSpendByNum")
    public boolean updateSpendByNum(@RequestBody SpendBean spendBean){

        //借款后修改图（统计图）
        int i = bankBillMapper.updateSpendByNum(spendBean);
        if(i > 0){
            return true;
        }
        return false;
    }


}
